package com.yxd.demo.util;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.RuleListener;
import org.springframework.stereotype.Component;

/**
 * @author: yxd
 * @date: 2022/8/2 9:19
 */
@Component
@Slf4j
public class MyRuleListener implements RuleListener {
    @Override
    public void afterEvaluate(Rule rule, Facts facts, boolean evaluationResult) {
        log.info("Rule条件判断之后--- 当前条件判断规则为：{}，条件判断为：{}", rule.getDescription(), evaluationResult);
    }

    @Override
    public boolean beforeEvaluate(Rule rule, Facts facts) {
        log.info("Rule条件判断之前判断当前规则是否跳过--- 当前条件判断规则为：{}", rule.getDescription());
        return true;
    }

    @Override
    public void onEvaluationError(Rule rule, Facts facts, Exception exception) {
        log.info("Rule条件判断时出现异常--- 当前条件判断规则为：{}", rule.getDescription());
    }

    @Override
    public void beforeExecute(Rule rule, Facts facts) {
        log.info("Rule执行操作之前--- 当前条件判断规则为：{}", rule.getDescription());
    }

    @Override
    public void onFailure(Rule rule, Facts facts, Exception exception) {
        log.info("Rule执行操作时出现异常--- 当前条件判断规则为：{}", rule.getDescription());
    }

    @Override
    public void onSuccess(Rule rule, Facts facts) {
        log.info("Rule执行操作成功的规则为：{}，fact为：{}", rule.getDescription(), JSONUtil.toJsonStr(facts));
    }
}
